//
//  ContentCell.m
//  植物管理
//
//  Created by liujianlong on 2022/11/21.
//

#import "ContentCell.h"
#import "ImgCell.h"

@interface ContentCell()
@property (strong, nonatomic) IBOutlet UITextView *input;
@property (strong, nonatomic) IBOutlet UILabel *placeholder;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionH;
@end
@implementation ContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.collectionView registerNib:[ImgCell nib] forCellWithReuseIdentifier:[ImgCell ID]];
}
- (void)setModel:(Job *)model{
    _model = model;
    self.input.text = self.model.remark;
    self.placeholder.hidden = self.model.remark.length > 0;
    self.collectionH.constant = self.model.imgs.count > 0 ? 200 : 0;
    [self.collectionView reloadData];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    self.placeholder.hidden = YES;
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    self.placeholder.hidden = textView.text.length > 0;
    self.model.remark = textView.text;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.imgs.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImgCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ImgCell ID] forIndexPath:indexPath];
    cell.img.image = [UIImage imageWithData:self.model.imgs[indexPath.row]];
    WS(self)
    cell.block = ^{
        [weakself.model.imgs removeObjectAtIndex:indexPath.row];
        [weakself.collectionView reloadData];
    };
    CGFloat w = (SCREEN_WIDTH - 40);
    CGSize size = CGSizeMake(w, 200);
    cell.bounds = CGRectMake(0, 0, size.width, size.height);
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 40);
    return  CGSizeMake(w, 200);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 20, 0, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
@end
